package com.test.byteDance;

import java.util.*;
import java.util.stream.Collectors;

public class Byte2019_3 {
    public List<Integer> maJiang(List<Integer> nums) {
        // 依次尝试
        List<Integer> res = new ArrayList<>();
        for (int i = 1; i <= 9; i++) {
            nums.add(i);
            if (huPai(nums)) {
                res.add(i);
            }
            nums.remove(nums.size() - 1);
        }
        res.sort(Comparator.comparingInt(o -> o));
        return res.size() == 0 ? Collections.singletonList(0) : res;
    }

    public boolean huPai(List<Integer> nums) {
        // 记录各个牌出现的个数
        int[] count = new int[10];
        for (int num : nums) {
            count[num]++;
        }
        // 取牌判断
        Map<Integer, List<Integer>> numMap = new HashMap<>();
        for (int i = 1; i < count.length; i++) {
            List<Integer> orDefault = numMap.getOrDefault(count[i], new ArrayList<>());
            orDefault.add(i);
            numMap.put(count[i], orDefault);
        }
        if (numMap.containsKey(3) && numMap.containsKey(2) && numMap.get(3).size() == 4 && numMap.get(2).size() > 0) {
            return true;
        }
        Set<Integer> collect = nums.stream().filter(key -> numMap.containsKey(1) && !numMap.get(1).contains(key)).collect(Collectors.toSet());
        for (Integer integer : collect) {
            count[integer] -= 2;
            if (counter(count) > 3) {
                return true;
            }
        }
        return false;
    }

    public int counter(int[] count) {
        int sum = 0;
        for (int i = 1; i < count.length - 2; i++) {
            while (count[i] > 0 && count[i + 1] > 0 && count[i + 2] > 0) {
                sum++;
                count[i]--;
                count[i + 1]--;
                count[i + 2]--;
            }
        }
        for (int i : count) {
            if (i>=3){
                sum++;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 13; i++) {
            list.add(scanner.nextInt());
        }
        Byte2019_3 byte2019_3 = new Byte2019_3();
        for (Integer integer : byte2019_3.maJiang(list)) {
            System.out.print(integer + " ");
        }
    }

}
